package www.yingdi365.com.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhy.magicviewpager.transformer.ScaleInTransformer;

import java.util.List;

import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseNotLeftActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.fragment.QingJianYLFragment;
import www.yingdi365.com.model.AppInvitationTmplPaging;
import www.yingdi365.com.model.AppInvitationTmplData;
import www.yingdi365.com.util.DpUtils;
import www.yingdi365.com.util.ScreenUtils;

public class QingJianYLActivity extends ZjbBaseNotLeftActivity implements View.OnClickListener {

    private ViewPager id_viewpager;
    private AppInvitationTmplPaging.TmplsBean tmplsBean;
    private List<AppInvitationTmplData.PagesBean> pagesBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qing_jian_yl);
        init();
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {
        Intent intent = getIntent();
        tmplsBean = (AppInvitationTmplPaging.TmplsBean) intent.getSerializableExtra(Constant.INTENT_KEY.TmplsBean);
        pagesBeanList = tmplsBean.getTmplDataEntity().getPages();
    }

    @Override
    protected void findID() {
        id_viewpager = (ViewPager) findViewById(R.id.id_viewpager);
    }

    @Override
    protected void initViews() {
        int screenWidth = ScreenUtils.getScreenWidth(this);
        ViewGroup.LayoutParams layoutParams = id_viewpager.getLayoutParams();
        float width = screenWidth-DpUtils.convertDpToPixel(100f, this);
        layoutParams.width = (int) width;
        layoutParams.height = (int) (width*1.4375f);
        id_viewpager.setLayoutParams(layoutParams);
        id_viewpager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        id_viewpager.setPageMargin((int) DpUtils.convertDpToPixel(20f,this));
        id_viewpager.setOffscreenPageLimit(3);
        id_viewpager.setPageTransformer(true, new ScaleInTransformer());
        ((TextView) findViewById(R.id.textViewTitle)).setText("请柬预览");
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.buttonLiJiZZ).setOnClickListener(this);
        findViewById(R.id.imageBack).setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageBack:
                finish();
                break;
            case R.id.buttonLiJiZZ:
                Intent intent = new Intent();
                intent.setClass(this, SheZhiQJActivity.class);
                intent.putExtra(Constant.INTENT_KEY.TmplsBean,tmplsBean);
                startActivity(intent);
                break;
        }
    }

    class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            String preview = pagesBeanList.get(position).getPreview();
            return new QingJianYLFragment(preview);
        }

        @Override
        public int getCount() {
            return pagesBeanList.size();
        }
    }
}
